/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.orderView;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.bean.TCommodity;
import com.ruipengkj.bean.TExeStageTmpl;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.component.JPanelGridLayoutComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.component.RPJButtonRoundBlue;
import com.ruipengkj.component.RPJButtonRoundOrager;
import com.ruipengkj.component.jpanel.QueryPanel;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.model.TExeStageTmplModel;
import com.ruipengkj.service.TExeStageTmplService;
import com.ruipengkj.view.base.BasePagePanel;
import com.ruipengkj.view.memberView.TExeStageTmplAdd;
import com.ruipengkj.view.memberView.TExeStageTmplUpdate;
/**
 * 订单评论
 */
public class TExeStageTmplPanel extends BasePagePanel<TExeStageTmpl> implements Runnable{

	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent orderNum; //订单号
	
	private JTable table; //订单Table
	
	private JButton queryButton; //查询按钮
	private JButton cancelButton;//清空按钮
	private JButton deleteButton = null;//删除按钮
	private JButton addButton = null;//添加按钮">
	
	private TExeStageTmplModel tExeStageTmplModel;
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TExeStageTmpl> pageView;//翻页主键
	private JScrollPane tableJsp;
	private TExeStageTmplService tExeStageTmplService;
	private TExeStageTmplAdd tExeStageTmplAdd;
	private TExeStageTmplUpdate tExeStageTmplUpdate;
	private TCommodity _tCommodity;
	private TExeStageTmpl tExeStageTmplEntity;
	public TExeStageTmplPanel(){ 
		setLayout(new BorderLayout());
		init();
		initListener();
		tExeStageTmplService=BuildSpringApplication.getInstance().getTExeStageTmplServiceImpl();
	}
	private void initListener() {
		// TODO Auto-generated method stub
		queryButton.addActionListener(this);
		cancelButton.addActionListener(this);
		addButton.addActionListener(this);
		deleteButton.addActionListener(this);
	}
	
	public void init() {
		
		pageView=new PageView<TExeStageTmpl>();
		tExeStageTmplModel=new TExeStageTmplModel();
		table=uiFactory.createRPJTable(tExeStageTmplModel, null, this, 50, JLabel.CENTER);
		tableJsp=uiFactory.createJScrollPane(table);
		pagingComponent=new PagingComponent();
		
		pagingComponent.setPageDelegale(this);
		this.add(topPanel(),BorderLayout.NORTH);
		this.add(tableJsp,BorderLayout.CENTER);
		this.add(pagingComponent,BorderLayout.SOUTH);
		this.add(initTail(),BorderLayout.SOUTH);
		//排序
		TableRowSorter<TExeStageTmplModel> tableRowSorter = new TableRowSorter<TExeStageTmplModel>(tExeStageTmplModel);  
		table.setRowSorter(tableRowSorter);
	}

	private JPanel topPanel(){
		QueryPanel queryPanel=new QueryPanel("商品阶段",ImageFactory.loadImage(ImageFactory.FUN_7_ICON_1));
		JPanel top_jp=new JPanel();
		top_jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		orderNum = new JLabelJTextFieldComponent("订单号：", 15);
		queryButton = new RPJButtonBlue("查询");
		cancelButton = new RPJButtonGray("清空");
		top_jp.add(orderNum);
		top_jp.add(queryButton);
		top_jp.add(cancelButton);
		queryPanel.addPanel(top_jp);
		return queryPanel;
	}
	private JPanel initTail() {
		addButton = new RPJButtonRoundBlue("添加");
		deleteButton = new RPJButtonRoundOrager("删除");
		JPanel jp2 = new JPanelFlowLayoutComponent(new Component[]{addButton,deleteButton}, FlowLayout.LEFT);
		JPanel tail_panel = new JPanelGridLayoutComponent(new Component[]{pagingComponent,jp2}, 2, 1);
		
		return tail_panel;
	}
	@Override
	public void run() {
		this.termQuery(1);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==queryButton){
			//查询
			this.termQuery(1);
			
		}else if(e.getSource()==cancelButton){
			//清除
			orderNum.getText().setText("");
		}else if(e.getSource()==addButton){
			//添加
			tExeStageTmplAdd=new TExeStageTmplAdd(null, "添加实施阶段数据", true);
			tExeStageTmplAdd.showDialog(_tCommodity);
			if(tExeStageTmplAdd.isSuccess){
				this.termQuery(1);
			}
		}else if(e.getSource()==deleteButton){
			int row = table.getSelectedRow();
			if (row != -1) {
				if (Util.showConfirmDialog("是否要删除选择记录") == 0) {
					String id=table.getValueAt(table.getSelectedRow(), -1).toString();
					try {
						tExeStageTmplService.deleteService(id);
						JOptionPane.showMessageDialog(null, "删除成功");
						this.termQuery(1);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
						JOptionPane.showMessageDialog(null, "删除失败");
					}
				}
			}else{
				JOptionPane.showMessageDialog(null, "请选择行在进行删除");
				return;
			}
		}
	}
	
	public void showPanel(TCommodity _c, boolean _isNew)
	{
		if(_c != null)
		{
			_tCommodity=_c;
		}
	}
	
	
	//条件查询
	@Override
	protected void termQuery(final int currentPage){
		final TExeStageTmpl tExeStageTmpl=new TExeStageTmpl();
		new Thread(){
			public void run(){
				SwingUtilities.invokeLater(new Runnable() {
					@Override
					public void run() {
						tableJsp.getViewport().remove(table);
						tableJsp.getViewport().add(waitComponent);
					}
				});				
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TExeStageTmpl> pageModel=tExeStageTmplService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tExeStageTmpl);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						tExeStageTmplModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								Util.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
						tExeStageTmplModel.clear();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}

	@Override
	protected PageView<TExeStageTmpl> getPageView() {
		return pageView;
	}
	
	// 鼠标双击 显示详细
	 
	@Override
	public void mouseClicked(MouseEvent e) {
    	if(e.getSource()==table&&e.getClickCount() == 2){
    		//表格
    		String id=table.getValueAt(table.getSelectedRow(), -1).toString();
    		try {
    			//添加
    			tExeStageTmplEntity=tExeStageTmplService.getByIdService(id);
    			tExeStageTmplUpdate=new TExeStageTmplUpdate(null, "修改实施阶段数据", true);
    			tExeStageTmplUpdate.showDialog(tExeStageTmplEntity);
    			if(tExeStageTmplUpdate.isSuccess){
    				this.termQuery(1);
    			}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
    	}
    }
	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
}
